package com.wyl.auth.common.controller;

import com.wyl.auth.common.service.RedisService;
import com.wyl.auth.dto.RedisDto;
import com.wyl.auth.req.KeyReq;
import com.wyl.auth.vo.RedisIndexVo;
import com.wyl.auth.vo.RedisKeyVo;
import com.wyl.auth.vo.RedisValueVo;
import com.wyl.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2025/4/20 22:12
 */
@Api(tags = "Redis控制台")
@RestController
@RequestMapping(value = "redis")
public class RedisController {

    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "redis相关指标")
    @GetMapping(value = "getRedisIndex")
    public ResponseData<RedisIndexVo> getRedisIndex() {
        return ResponseData.getSuccessInstance(redisService.getRedisIndex());
    }

    @ApiOperation(value = "获取键集合")
    @PostMapping(value = "listKeys")
    public ResponseData<List<RedisKeyVo>> listKeys(@RequestBody KeyReq keyReq) {
        return ResponseData.getSuccessInstance(redisService.listKeys(keyReq));
    }

    @ApiOperation(value = "获取值")
    @GetMapping(value = "getValue")
    public ResponseData<RedisValueVo> getValue(String key) {
        return ResponseData.getSuccessInstance(redisService.getValue(key));
    }

    @ApiOperation(value = "保存key的值")
    @PostMapping(value = "save")
    public ResponseData save(@RequestBody RedisDto redisDto) {
        redisService.save(redisDto);
        return ResponseData.getSuccess();
    }

    @ApiOperation(value = "更新ttl")
    @PostMapping(value = "updateTTL")
    public ResponseData<Boolean> updateTTL(@RequestBody RedisDto redisDto) {
        return ResponseData.getSuccessInstance(redisService.updateTTL(redisDto));
    }

    @ApiOperation(value = "删除key")
    @PostMapping(value = "deleteKey")
    public ResponseData deleteKey(@RequestBody List<String> keys) {
        redisService.deleteKey(keys);
        return ResponseData.getSuccess();
    }
}